#include "GMesh.h"


GMesh::GMesh(){
    clearMesh();
}

void GMesh::getMesh(std::string filename){
    std::istringstream iss;
    std::ifstream file{filename, std::ios::in};
    std::string line;

    while (std::getline(file, line)) {
        if(line == "$Nodes"){
            std::getline(file, line);
            while (line != "$EndNodes") {  //提取结点信息
                iss.clear();
                iss.str(line);
                iss >> numEntityBlocks >> numNodes >> minNodeTag >> maxNodeTag;
                int index_coord = 0;
                uint Numcoord_tmp{0};
                while(index_coord != maxNodeTag){
                    std::getline(file, line);
                    iss.clear();
                    iss.str(line);
                    iss >> entityDim >> entityTag >> parametric >> numNodesInBlock;
                    for(int index_tmp = 0; index_tmp < numNodesInBlock; index_tmp++){
                        std::getline(file, line);
                    }
                    for(int index_numNodesInBlock = 0; index_numNodesInBlock < numNodesInBlock; index_numNodesInBlock++){
                        std::getline(file, line);
                        iss.clear();
                        iss.str(line);
                        std::vector<double> coord_temp{};
                        coord_temp.push_back(static_cast<double> (++Numcoord_tmp));
                        for(int i = 0; i < 3; i++){
                            double tmp;
                            iss >> tmp;
                            coord_temp.push_back(tmp);
                        }
                        coord.push_back(coord_temp);
                    }
                    index_coord += numNodesInBlock;
                }
                std::getline(file, line);
            }
        }
        if(line == "$Elements"){
            std::getline(file, line);
            while (line != "$EndElements") {
                //do something with line
                iss.clear();
                iss.str(line);
                int tmp;
                iss >> tmp >> numElements >> minElementTag >> maxElementTag;
                int index_element = 0;
                while(index_element != maxElementTag){
                    std::getline(file, line);
                    iss.clear();
                    iss.str(line);
                    iss >> entityDim >> entityTag >> elementType >> numElementsInBlock;
                    for(int index_tmp = 0; index_tmp < numElementsInBlock; index_tmp++){
                        std::getline(file, line);
                        iss.clear();
                        iss.str(line);
                        std::vector<uint> element_temp{entityTag};
                        for(int i = 0; i < entityDim+2; i++){
                            uint tmp;
                            iss >> tmp;
                            element_temp.push_back(tmp);
                        }
                        if(entityDim == 0){
                            element_P.push_back(element_temp);
                        }else if(entityDim == 1){
                            element_L.push_back(element_temp);
                        }else if(entityDim == 2){
                            element_S.push_back(element_temp);
                        }else if(entityDim == 3){
                            element_V.push_back(element_temp);
                        }
                    }
                    index_element += numElementsInBlock;
                }
                std::getline(file, line);
            }
        }
    }
    file.close();
}

void GMesh::clearMesh(){
    coord.clear();
    element_P.clear();
    element_L.clear();
    element_S.clear();
    element_V.clear();
    numEntityBlocks = numNodes = minNodeTag = maxNodeTag = 0;
    entityDim = entityTag = parametric = numElementsInBlock = 0;
    numElements = minElementTag = maxElementTag = 0;
    elementType = numElementsInBlock = 0;
}